import { h, ref } from 'vue';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';
import { useDictStore } from '@/store/modules/dict';

const dict = useDictStore();

export class State {
  public id = 0; // 主键id
  public productId = 0; // 商品id
  public name = []; // 属性名称
  public value = ''; // 属性值
  public deletedAt = ''; // deleted_at

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  name: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入属性名称',
  },
  value: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入属性值',
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'id',
    component: 'NInputNumber',
    label: '主键id',
    componentProps: {
      placeholder: '请输入主键id',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: '主键id',
    key: 'id',
    align: 'left',
    width: -1,
  },
  {
    title: '商品id',
    key: 'productId',
    align: 'left',
    width: -1,
  },
  {
    title: '属性名称',
    key: 'name',
    align: 'left',
    width: -1,
  },
  {
    title: '属性值',
    key: 'value',
    align: 'left',
    width: -1,
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['epCategoryOption']);
}